package com.chenshu.lifecycle.beanlife;

import com.chenshu.lifecycle.model.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-04
 * Time：23:27
 */
@Component
public class BeanLifeComponent implements BeanNameAware, InitializingBean {
    private User user;
    //构造方法
    public BeanLifeComponent() {
        System.out.println("执行构造方法");
    }
    //setter依赖注入
    @Autowired
    public void setUser(User user) {
        this.user = user;
        System.out.println("依赖注入");
    }
    //BeanNameAware的通知方法
    @Override
    public void setBeanName(String name) {
        System.out.println("执行了BeanName通知方法，name="+name);
    }
    //初始化方法
    @PostConstruct
    public void initByAnnotation() {
        System.out.println("执行@PostConstrut修饰的init方法");
    }
    //初始化方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行InitializingBean接口的afterPropertiesSet方法");
    }
    //初始化方法
    public void initByXml() {
        System.out.println("执行xml中定义的init方法");
    }
    //使用Bean
    public void use() {
        System.out.println("使用use方法");
    }
    //销毁前调用的方法
    @PreDestroy
    public void destroy() {
        System.out.println("执行@PreDestroy修饰的destroy方法");
    }
}
